# -*- coding:UTF-8 -*-
'''
开发环境：PyCharm
创建日期：2025/8/21 16:16
作   者：新启典学苑-樱桃
描   述：
'''
import pymysql

from jinrongApiAutoTest.util.log import Logger
from jinrongApiAutoTest.util.readfile import read_ini


def connect(host, port, user, pwd, dbname):
    try:
        conn = pymysql.connect(user=user,
                               password=pwd,
                               host=host,
                               database=dbname,
                               port=int(port),
                               charset="UTF8")
        Logger().info(f"连接数据库{host}:{port}成功")
        return conn
    except Exception as e:
        Logger().error(f"连接数据库{host}:{port}异常，异常信息为：{e}")


def execute(conn, sql):
    try:
        c = conn.cursor()  # 获取游标
        c.execute(sql)
        conn.commit()
        r = c.fetchall()
        c.close()  # 关闭游标
        Logger().info(f"执行{sql}语句成功")
        return r
    except Exception as e:
        Logger().error(f"执行{sql}语句异常，异常信息为：{e}")


def disconnect(conn):
    conn.close()


# 从配置文件中读取数据库相关的信息
host = read_ini("data/env.ini", 'env', 'dbhost')
port = read_ini("data/env.ini", 'env', 'dbport')
user = read_ini("data/env.ini", 'env', 'dbuser')
pwd = read_ini("data/env.ini", 'env', 'dbpwd')
dbname = read_ini("data/env.ini", 'env', 'dbname')


def delete_by_phone(phone):
    c = connect(host, port, user, pwd, dbname)
    execute(c, f"delete from member where mobilephone={phone}")
    disconnect(c)

def get_amount_by_phone(phone):
    c = connect(host, port, user, pwd, dbname)
    r = execute(c, f"select leaveamount from member where mobilephone={phone}")
    # print(r)
    # ((Decimal('2509000.02'),),)    a = (1) 是整数  a=(1,) 是元组
    # (Decimal('2509000.02'),)
    # Decimal('2509000.02')
    disconnect(c)
    return r[0][0]

def get_id_by_phone(phone):
    c = connect(host, port, user, pwd, dbname)
    r = execute(c, f"select id from member where mobilephone={phone}")
    disconnect(c)
    return r[0][0]

# 测试代码，用完删除   insert、delete update 返回的是影响的行数  select 返回表格
if __name__ == '__main__':
    a = get_amount_by_phone("18012345678")
    print(type(a))
    print(a)
    print(float(a) == 2509000.02)
